package cn.crm.web.controller.business;

import cn.crm.common.annotation.Log;
import cn.crm.common.core.controller.BaseController;
import cn.crm.common.core.domain.AjaxResult;
import cn.crm.common.core.page.TableDataInfo;
import cn.crm.common.enums.BusinessType;
import cn.crm.system.domain.pojo.dto.BusinessDto;
import cn.crm.system.domain.pojo.dto.UploadingDto;
import cn.crm.system.domain.pojo.vo.UploadingVo;
import cn.crm.system.service.impl.BusinessServiceImpl;
import cn.crm.system.service.impl.UploadingServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/uploading")
@Api(tags = "上传模块")
public class UploadingController extends BaseController {

    @Autowired
    private UploadingServiceImpl uploadingService;

    public UploadingController() {
        log.debug("创建控制器对象：UploadingController");
    }

    @PreAuthorize("@ss.hasPermi('system:uploading:add')")
    @PostMapping("/add")
    @ApiOperation("上传资料")
    @Log(title = "上传模块管理", businessType = BusinessType.INSERT)
    public AjaxResult addBusiness(@RequestBody UploadingDto uploadingDto) {
        log.debug("开始处理上传模块资料的接口");
        int i = uploadingService.insertUploading(uploadingDto);
        if (i < 1){
            return AjaxResult.error("上传文件失败");
        }
        return AjaxResult.success("上传成功");
    }

    @PreAuthorize("@ss.hasPermi('system:uploading:query')")
    @GetMapping("/query")
    @ApiOperation("查询上传资料")
    @Log(title = "上传模块管理", businessType = BusinessType.OTHER)
    public TableDataInfo queryBusiness(UploadingDto  uploadingDto ) {
        log.debug("开始处理查询上传模块资料的接口");
        startPage();
        List<UploadingVo> uploadingVos = uploadingService.selectUploading(uploadingDto);
        return  getDataTable(uploadingVos);

    }

    @PreAuthorize("@ss.hasPermi('system:uploading:delete')")
    @DeleteMapping("/{id}")
    @ApiOperation("删除上传资料")
    @Log(title = "上传模块管理", businessType = BusinessType.OTHER)
    public AjaxResult delete(@PathVariable Long[] id){
        log.debug("开始删除资料的接口");
        int i = uploadingService.deleteUploading(id);
        if (i < 1){
            return AjaxResult.error("删除资料失败");
        }

        return AjaxResult.success("删除资料成功");
    }

}
